define(['jquery', 'bootstrap', 'backend', 'table', 'form','printarea'], function ($, undefined, Backend, Table, Form) {
    var tableData={
        info_list:[{order_no:'1111',trademark_printing:'1111',colour:'1111',employee:'1111'}],
        materiallist:[{material:'1111',material_colour:'1111',weight:'1111'}]
    };
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'entrepot/deliver/index' + location.search,
                    add_url: 'entrepot/deliver/add',
                    del_url: 'entrepot/deliver/del',
                    multi_url: 'entrepot/deliver/multi',
                    import_url: 'entrepot/deliver/import',
                    deliver_url: 'entrepot/deliver/deliver',
                    table: 'deliver',
                },
                pageSize: 15,
                pageList: [15]
            });

            var table = $("#table");

            //点击验证弹框打印
            $(".btn-deliver").click(function(){
                var temp=table.bootstrapTable('getSelections');
                let ids = '';
                var arr=[];
                temp.map(item=>{
                    arr.push(item.id);
                })
                ids = arr.join(',');

                Fast.api.ajax({
                    url: 'entrepot/deliver/check_info',
                    type: 'post',
                    data:{ids:ids},
                }, function (data, ret) {
                },function (data, ret) {
                    if(ret.res==1){
                        //成功
                        Fast.api.open('entrepot/deliver/deliver?ids='+ids,'发料信息预览', {
                            callback:function(value){
                                $(".btn-refresh").trigger("click");//刷新当前页面的数据
                            }
                        });
                    }else{
                        //失败提示
                        Layer.confirm(ret.msg, {icon: 3, title: __('提示')}, function () {
                            Layer.closeAll();
                        });
                    }
                    return false;
                });

            });
            //退料
            $(".btn-withdraw").click(function(){
                var temp=table.bootstrapTable('getSelections');
                let ids = '';
                var arr=[];
                temp.map(item=>{
                    arr.push(item.id);
                })
                ids = arr.join(',');
                layer.confirm('确定执行退料操作？', {
                }, function(index, layero){
                    Fast.api.ajax({
                        url: 'entrepot/deliver/withdraw',
                        type: 'post',
                        data:{ids:ids},
                    }, function (data, ret) {
                        layer.close(index);
                    });
                    layer.close(index);
                });

            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                dblClickToEdit: false,
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        {field: 'order.order_no', title: __('Order.order_no'), operate: 'LIKE'},
                        {field: 'detail.trademark_printing', title: __('Detail.trademark_printing'), operate: 'LIKE'},
                        {field: 'detail.abbreviation', title: __('Detail.abbreviation'), operate: 'LIKE'},
                        {field: 'colour.name', title: __('色号'), operate: 'LIKE'},
                        {field: 'employee.name', title: __('Employee.name'), operate: 'LIKE'},
                        {field: 'delivertime', title: __('发料时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'plan.name', title: __('物料方案'),formatter: function (value,row,index){
                                return selectGroup(value,row,index)
                        }},
                        {field: 'quantity', title: __('Quantity')},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'detail.quantity', title: __('Detail.quantity')},
                        {field: 'xiangqing', title: __('详情'), table: table,formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: '查看',
                                    title: '查看',
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-xs btn-info btn-detail btn-dialog',
                                    url: 'entrepot/deliver/edit?type=deliverdetail'
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });
            function selectGroup (value,row,index) {   // 自定义方法，添加下拉按钮
                let url = '/jiafa.php/entrepot/deliver/edit/ids/'+row.id;

                let html = '<a href = "'+url+'" class="btn-edit btn-dialog" title="编辑物料方案" >' +
                    row.plan.name+'</a>';
                return html
        };

            // 自定义导出按钮
            $(document).on("click", ".btn-myexcel-export", function () {
                var page = table.bootstrapTable('getData'); // 获取页面数据
                let ids = [];
                let id_data = '';
                // 取出当前页面的id
                $.each(page,function(i,v){
                    if(i == 0){
                        id_data = v.id;
                    }else{
                        id_data += ','+v.id;
                        ids.push(v.id)
                    }
                    console.log('选择数据',i)
                })
                layer.confirm("请选择导出的选项", {
                    title: '导出数据',
                    btn: ['全部数据','当前页数据(' + ids.length + '条)'],
                    yes: function (index, layero) {
                        // 全部数据
                        Layer.close(index);
                        top.location.href="/xxx/xx/export?id_data="
                    },
                    btn2: function (index, layero) {
                        // 当前页面数据
                        top.location.href="/xxx/xx/export?id_data="+id_data
                    }
                })
            });
            // 为表格绑定事件
            Table.api.bindevent(table);

        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'entrepot/deliver/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'entrepot/deliver/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'entrepot/deliver/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            $(document).on(function(){
                Form.events.selectpage($("#c-material_plan_id"));
            });
            Controller.api.bindevent();
        },
        edit: function () {
            $(document).on(function(){
                Form.events.selectpage($("#c-material_plan_id"));
            });
            Controller.api.bindevent();
        },
        deliver:function () {
            Controller.api.bindevent();
            var ids = $('#ids').val();
            $('.print-table').click(function () {
                showPrint();
            })
            function showPrint(data){
                $('#table-print').printArea();
            }
            $('.deliver-ajax').click(function () {
                //请求发料数据
                Fast.api.ajax({
                    url: 'entrepot/deliver/deliver',
                    type: 'post',
                    data:{ids:$('#ids').val()},
                }, function (data, ret) {
                    setTimeout(function () {
                        Fast.api.close();
                    },2000)
                    Layer.alert(ret.msg, {icon: 6});
                    $(".btn-refresh").trigger('click');//刷新当前页面的数据
                });
            })
            $('.close-btn').click(function () {
                Fast.api.close()
            })
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
